package com.ctshk.rpc.ship.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.ship.dto.ShipConstants;
import com.ctshk.rpc.ship.dto.ShipRouteDTO;
import com.ctshk.rpc.ship.entity.ShipRoute;
import com.ctshk.rpc.ship.mapper.ShipRouteMapper;
import com.ctshk.rpc.ship.req.ShipRoutePageReq;
import com.ctshk.rpc.ship.req.ShipRouteReq;
import com.ctshk.rpc.ship.service.IShipRouteService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 船-航线表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-05
 */
@DubboService
public class ShipRouteServiceImpl extends ServiceImpl<ShipRouteMapper, ShipRoute> implements IShipRouteService {

    @Autowired
    private ShipRouteMapper ShipRouteMapper;

    @Override
    public PageResponse<ShipRouteDTO> queryShipRouteList(ShipRoutePageReq req) {
        IPage<ShipRoute> iPage = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<ShipRoute> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ShipRoute::getIsDeleted, ShipConstants.NOT_DELETE).orderByDesc(ShipRoute::getGmtCreate);

        //查询条件
        if (!StringUtils.isEmpty(req.getCompanyType())) {
            queryWrapper.lambda().like(ShipRoute::getCompanyType, req.getCompanyType());
        }

        if (!StringUtils.isEmpty(req.getRouteName())) {
            queryWrapper.lambda().like(ShipRoute::getRouteName, req.getRouteName());
        }

        if (!StringUtils.isEmpty(req.getDepPlaceName())) {
            queryWrapper.lambda().like(ShipRoute::getDepPlaceName, req.getDepPlaceName());
        }
        if(req.getDepCityId() != null){
            queryWrapper.lambda().eq(ShipRoute::getDepCityId, req.getDepCityId());
        }
        if(req.getArrCityId() != null){
            queryWrapper.lambda().eq(ShipRoute::getArrCityId, req.getArrCityId());
        }
        if (!StringUtils.isEmpty(req.getStatus())) {
            queryWrapper.lambda().eq(ShipRoute::getStatus, req.getStatus());
        }

        IPage<ShipRoute> ShipRouteIPage = baseMapper.selectPage(iPage, queryWrapper);

        if (!CollectionUtils.isEmpty(ShipRouteIPage.getRecords())) {
            List<ShipRouteDTO> ShipRouteDTOList = EntityUtil.copyList(ShipRouteIPage.getRecords(), ShipRouteDTO.class);
            return new PageResponse<>(ShipRouteDTOList, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }

        return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());

    }

    @Override
    public Result<ShipRouteDTO> queryShipRouteDetail(ShipRoutePageReq req) {
        if (null == req) {
            return Result.failed(SystemError.SHIP_PRODUCT_11015);
        }
        ShipRoute shipRoute = ShipRouteMapper.selectById(req.getId());
        ShipRouteDTO shipRouteDTO = new ShipRouteDTO();
        BeanUtils.copyProperties(shipRoute, shipRouteDTO);
        return Result.success(shipRouteDTO);
    }

    @Override
    public Result saveShipRoute(ShipRouteReq shipRouteReq, TokenUser user) {
        if (!ObjectUtils.isEmpty(user)) {
            Result.failed(SystemError.USER_1000);
        }
        Result checkResult = checkShipRouteData(shipRouteReq);
        if (!Objects.isNull(checkResult)) {
            return checkResult;
        }
        ShipRoute shipRoute = new ShipRoute();
        BeanUtils.copyProperties(shipRouteReq, shipRoute);
        shipRoute.setGmtCreate(LocalDateTime.now());
        shipRoute.setId(SnowflakeIdWorker.nextId());
        shipRoute.setStatus(ShipConstants.VALID);
        return Result.success(ShipRouteMapper.insert(shipRoute));
    }

    @Override
    public Result updateShipRoute(ShipRouteReq shipRouteReq, TokenUser user) {
        Result checkResult = checkShipRouteData(shipRouteReq);
        if (!Objects.isNull(checkResult)) {
            return checkResult;
        }
        ShipRoute shipRoute = ShipRouteMapper.selectById(shipRouteReq.getId());
        BeanUtils.copyProperties(shipRouteReq, shipRoute);
        shipRoute.setModifiedId(user.getId());
        shipRoute.setGmtModified(LocalDateTime.now());
        return Result.success(ShipRouteMapper.updateById(shipRoute));
    }

    @Override
    public Result updateShipRouteStatus(ShipRouteReq shipRouteReq, TokenUser user) {
        if (ObjectUtils.isEmpty(shipRouteReq)) {
            Result.failed(SystemError.SHIP_PRODUCT_11015);
        }
        ShipRoute shipRoute = ShipRouteMapper.selectById(shipRouteReq.getId());
        shipRoute.setStatus(shipRouteReq.getStatus());
        shipRoute.setGmtModified(LocalDateTime.now());
        shipRoute.setModifiedId(user.getId());
        return Result.success(ShipRouteMapper.updateById(shipRoute));
    }


    private Result checkShipRouteData(ShipRouteReq busProductReq) {
        if (!StringUtils.isEmpty(busProductReq.getRouteName())) {
            Result.failed(SystemError.SHIP_PRODUCT_11007);
        }
        if (!StringUtils.isEmpty(busProductReq.getArrCityName())) {
            Result.failed(SystemError.SHIP_PRODUCT_11009);
        }
        if (!StringUtils.isEmpty(busProductReq.getDepCityName())) {
            Result.failed(SystemError.SHIP_PRODUCT_11008);
        }
        if (!StringUtils.isEmpty(busProductReq.getArrPlaceName())) {
            Result.failed(SystemError.SHIP_PRODUCT_11013);
        }
        if (!StringUtils.isEmpty(busProductReq.getDepPlaceName())) {
            Result.failed(SystemError.SHIP_PRODUCT_11012);
        }
        if (!StringUtils.isEmpty(busProductReq.getCompanyType())) {
            Result.failed(SystemError.SHIP_PRODUCT_11014);
        }
        if (!StringUtils.isEmpty(busProductReq.getCabinClass())) {
            Result.failed(SystemError.SHIP_PRODUCT_11010);
        }
        if (!StringUtils.isEmpty(busProductReq.getTouristTypeId())) {
            Result.failed(SystemError.SHIP_PRODUCT_11011);
        }
        return null;
    }
}
